package com.mytest.common.jasypt;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * @author cxb
 * @date 2019-07-25 15:28
 */
public class JasyptUtil {

    /**
     * Jasypt生成加密结果
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待加密值
     * @return
     */
    public static String encyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        String result = encryptor.encrypt(value);
        return result;
    }

    /**
     * 解密
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待解密密文
     * @return
     */
    public static String decyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        String result = encryptor.decrypt(value);
        return result;
    }

    public static SimpleStringPBEConfig cryptor(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }

    public static void main(String[] args) {
        //nebula-official

        //加密
//        System.out.println(encyptPwd("IepoB8l4UoM@nE&j", "127.0.0.1"));
//        System.out.println(encyptPwd("IepoB8l4UoM@nE&j", "Cpic1234"));
//        System.out.println(encyptPwd("IepoB8l4UoM@nE&j", "Cpic1234"));
//        System.out.println(encyptPwd("IepoB8l4UoM@nE&j", "Cpic1234wsehfgfjrdgsgbngfcnmEWGER"));
        //解密
//        System.out.println(decyptPwd("IepoB8l4UoM@nE&j","bnEGlf/CHEXjk0rWxst0sfRSYvpDlLbh"));
//        System.out.println(decyptPwd("IepoB8l4UoM@nE&j","mpRWGEfuoAtPKyY4jLrcTH0nmF+prMQC"));
        System.out.println("a"+decyptPwd("IepoB8l4UoM@nE&j","ENC(LShBNSq6WP1jCb7YHvDAEQ==)"));
        System.out.println("a"+decyptPwd("IepoB8l4UoM@nE&j","ENC(I8PkwICJHBNXCMTjJNHfwjPs65tFpMPv)"));



    }
}